import transferData from '@/tools/transferData.js'
import Topbar from '@/components/topbar/topbar.vue'
export default {
    components: {
        Topbar
    },
    data() {
        return {
            phone: '',
            password: '',
            passwordType: 'password',
            errorPhoneTxt: '',
            errorPasswordTxt: ''
        }
    },
    methods: {
        // 过滤非数字
        formatter(value) {
            return value.replace(/[^0-9]/ig, '');
        },
        // 验证号码
        validateNumber() {
            if (this.phone === '') {
                this.errorPhoneTxt = '请输入手机号码'
                return false
            } else {
                let flag = /^((13|15|18|14|17|19)+\d{9})$/.test(this.phone)
                if (!flag) {
                    this.errorPhoneTxt = '手机号码格式不正确'
                    return false
                }
            }
            this.errorPhoneTxt = ''
            return true
        },
        // 验证密码
        validatePsd() {
            if (this.password === '') {
                this.errorPasswordTxt = '请输入密码'
                return false
            } else {
                let flag = /^\w{6,18}$/.test(this.password)
                if (!flag) {
                    this.errorPasswordTxt = '密码不能少于6位'
                    return false
                }               
            }
            this.errorPasswordTxt = ''
            return true
        },
        // 注册
        onSubmit(values) {
            console.log(JSON.stringify(values))
            if (this.validateNumber() && this.validatePsd()) {
                transferData.userRegister(values).then(({ data }) => {
                    if (data.code == 200) {
                        this.$notify({ type: 'success', message: '注册成功', duration: 1000 });                       
                        let that = this
                        setTimeout(function(){
                            that.$router.push('/login')
                        },1000);               
                    } else {
                        this.$notify({ type: 'danger', message: '用户已存在', duration: 1000 });
                    }
                })
            }
        },
        switchPasswordType() {
            this.passwordType = this.passwordType === 'password' ? 'text' : 'password'
        }
    }
}